//服务器地址
import {server} from '../config/index.js';

castapp.init();

var inpElems = document.querySelectorAll('input');
var confirmBtnEle = document.querySelector('.login-btn button');

var userInfo = localStorage.getItem('userInfo');
userInfo = userInfo? JSON.parse(userInfo): {};
var nickname = userInfo.nickname;
var BB_num = userInfo.BB_num;

document.querySelector('.bbnum').innerHTML = BB_num;

confirmBtnEle.addEventListener('tap', function(e){
	e.preventDefault();
	var password = inpElems[0].value.trim();  //原密码
	var password1 = inpElems[1].value.trim();  //新密码
	var password2 = inpElems[2].value.trim();  //确认新密码
	
	if(!password || !password1 || !password2){
		castapp.prompt('密码输入不能为空');
		return;
	}
	
	if(password1 != password2){
		castapp.prompt('两次密码输入不一致');
		return;
	}
	
	for(var i=0; i<inpElems.length; i++){
		if(!inpElems[i].value){
			inpElems[i].focus();
			return;
		}
	};
	
	var nickname = localStorage.getItem('nickname');
	castapp.ajax({
		url: server+ '/api/user/modify_password',
		type:'post',
		data:{
		    nickname,
		    origin_pass: password,
		    new_pass: password1
		},
		succFn:function(res){
			res = JSON.parse(res);
		    if(res.msg=='OK'){
		    	castapp.prompt('修改成功');
		    	castapp.closeCurrentInterface();
		    }else{
		    	castapp.prompt('原密码错误，请重新输入');
		    }
		},
		errFn:function(err){
		    castapp.prompt('服务错误，请重试');
		}
	});
});

